package com.example.erp;

import java.util.Scanner;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class AiApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(AiApplication.class, args);
		System.err.println("====== ai application started... ======");
	}

	@GetMapping("/home")
	public String home() {
		return "<h1>Hello World! Welcome to Spring Boot AI...</h1>";
	}

	// 控制台对话
	@Bean
	CommandLineRunner cli(ChatClient.Builder builder) {
		return args -> {
			var chat = builder.build();
			try (var scanner = new Scanner(System.in)) {
				System.out.println("\nLet's chat!");
				while (true) {
					System.out.print("\nUSER: ");
                	System.out.println("ASSISTANT: " + chat.prompt(scanner.nextLine()).call().content());
					// 流式返回似乎不支持
//					System.out.println("ASSISTANT: " + chat.prompt(scanner.nextLine()).stream().content());
				}
			}
		};
	}
}
